package com.caigou.comm.base.widget

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.Window
import android.view.WindowManager
import android.widget.TextView
import com.aigestudio.wheelpicker.WheelPicker
import com.caigou.comm.R
import com.caigou.comm.base.ext.getScreenWidth
import java.util.*

/**
 * @author #Suyghur.
 * Created on 2021/11/09
 */
class DatePickDialog constructor(context: Context) : Dialog(context) {

    private val years = mutableListOf<Int>()
    private val months = mutableListOf<Int>()

    lateinit var tvCancel: TextView
    lateinit var tvConfirm: TextView
    lateinit var wpYear: WheelPicker
    lateinit var wpMonth: WheelPicker

    init {
        setCanceledOnTouchOutside(false)
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        initView()
        initData()
    }

    private fun initView() {
        setContentView(R.layout.comm_date_pick)
        val attr = window?.attributes as WindowManager.LayoutParams
        //设置dialog在布局中的位置
        attr.gravity = Gravity.BOTTOM
        attr.width = getScreenWidth(context)
        tvCancel = findViewById(R.id.date_pick_dialog_tv_cancel)
        tvConfirm = findViewById(R.id.date_pick_dialog_tv_confirm)
        wpYear = findViewById(R.id.date_pick_dialog_wp_year)
        wpMonth = findViewById(R.id.date_pick_dialog_wp_month)
    }

    private fun initData() {
        val currentYear = Calendar.getInstance().get(Calendar.YEAR)
        for (year in currentYear until (currentYear + 5)) {
            years.add(year)
        }
        for (month in 1 until 13) {
            months.add(month)
        }
        wpYear.data = years
        wpMonth.data = months
    }

    fun getDate(): String {
        return "${years[wpYear.currentItemPosition] - months[wpMonth.currentItemPosition]}"
    }
}